package com.zbkj.service.assistant.service;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.assistant.AssistantGroup;
import com.zbkj.common.result.AssistantResultCode;
import com.zbkj.service.assistant.dao.AssistantGroupDao;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
* @author zhuwy
* @description AssistantGroupServiceImpl 接口实现
* @date 2025-06-18
*/
@Service
public class AssistantGroupBaseServiceImpl extends ServiceImpl<AssistantGroupDao, AssistantGroup> {

    /**
     * 获取默认分组，默认分组无法删除
     * @return
     */
    public AssistantGroup getDefault() {
        return super.lambdaQuery().eq(AssistantGroup::getIsDefault, true).one();
    }

    /**
     * 默认分组禁止删除
     * @return
     */
    public boolean removeById(Serializable id) {
        AssistantGroup dbGroup = super.getById(id);
        if (ObjUtil.isNotEmpty(dbGroup) && dbGroup.getIsDefault())
            throw new CrmebException(AssistantResultCode.GROUP_DEFAULT_NOT_DELETE);
        return super.removeById(id);
    }

    /**
     * 根据分组代码获取分组
     * @param codeEnum
     * @return
     */
    public AssistantGroup getByCode(AssistantGroup.CodeEnum codeEnum) {
        return super.lambdaQuery().eq(AssistantGroup::getCode, codeEnum.getCode()).one();
    }
}

